home *** CD-ROM | disk | FTP | other *** search
- {
- I've gotten a couple requests for this source, which quasi-intelligently
- converts Unix-format text to DOS-format text and vice versa. Recently,
- I just
- added a better command-line interpreter, and cleaned it up a little. I was
- hoping to get around to using untyped files instead of text files, but maybe
- later.
-
- This is probably not the most graceful (and since it uses text files,
- not the fastest way to do this), but it's worked well for me.
- Suggestions on how to improve are welcome.
-
- -Scott E.
- tiobe@cmu.edu
- ------------------------------------------------------------------}
- program SConvert;
-
- {Smart-converts UN*X/DOS format files
-
- Usage: sconvert infile [outfile] [/U | /D]
- [/U forces unix, /D forces DOS, if forced type, do nothing.]
-
- -- or --
-
- sconvert /? (-?, /h, -h, /H, and -H analogous)
- for help message
-
- This program is capabable of having its output piped, provided
- it is the first in the pipeline. Since it opens input twice,
- using it anywhere in a pipe besides the beginning probably won't
- work well.
-
- Written by Scott F. Earnest, Aug 1993
- Original version: 30 Aug 1993
- Updated version: 9 May 1994 (Added force flags.)
- }
-
- uses Crt;
-
- const
- CR = chr(13); {Carriage Return}
- LF = chr(10); {Line Feed}
-
- type
- sys = (dos,unix,bad); {system identifier}
-
- var
- sysID : sys; {system identifier for case branch}
- infile, outfile : string; {input/output files}
- force : sys; {What mode to work in.}
-
- function exist (filename : string) : boolean;
-
- {Check if a file exists or not
- returns: true --> file exists
- false --> file non-existent}
-
- var
- openfile : text;
- errcode : integer;
-
- begin
- {$I-} {Turn off error-checking}
- assign (openfile, filename);
- reset (openfile);
- {$I+} {Turn it back on}
- errcode := IOResult; {Get error code}
- if errcode <> 0 then {There's an error if non-zero}
- exist := false {So flag that it doesn't exist.}
- else
- begin
- close (openfile); {Otherwise, close file}
- exist := true; {Flag that it does exist}
- end;
- end;
-
- function selectyn : boolean;
-
- {Get a yes/no single-keypress response
- returns: true --> yes response, y or Y
- false --> no response, n or N}
-
- var
- getchar : char; {Need something to read into}
-
- begin
- while KeyPressed do {Clean keyboard buffer}
- getchar := ReadKey;
- repeat {Get a key until it's a (Y)es or (N)o.}
- getchar := ReadKey;
- getchar := upcase (getchar);
- until (getchar in ['Y', 'N']);
- writeln (getchar); {Print the response}
- case getchar of {Tell it what it should return}
- 'Y' : selectyn := true;
- 'N' : selectyn := false;
- end;
- end;
-
- procedure help (badflag : boolean);
-
- {brief message if command format was abused}
-
- begin
- writeln ('SmartConvert, Written by Scott F. Earnest -- v1.3 -- 9 May 1994');
- writeln;
- if badflag then
- begin
- writeln ('Invalid flag.');
- writeln;
- end;
- writeln ('Usage');
- writeln (' sconvert infile [outfile] [/d | /u]');
- writeln;
- writeln ('Use /d to force conversion to DOS, and /u to force UNIX.');
- halt (1);
- end;
-
- procedure incheck (filename : string);
-
- {Make sure source exists, if specified}
-
- begin
- if not (exist (filename)) then
- begin
- writeln ('Source file does not exist!');
- halt (3);
- end;
- end;
-
- procedure outcheck (filename : string);
-
- {Make sure target does NOT exist, if specified, allow overwrite}
-
- var
- select : boolean;
-
- begin
- if exist (filename) and (filename <> '') then
- begin
- write ('Target file exists! Overwrite? [y/n] ');
- select := selectyn;
- case select of
- true : ;
- false : halt (4);
- end;
- end;
- end;
-
- function checktype (readfile : string) : sys;
-
- var
- FileCheck : text;
- checkvar : sys;
- CROk, LFOk : boolean;
- ReadBuf : char;
-
- begin
- CROk := False;
- LFOk := False; {Init flags.}
- checkvar := bad; {Assume that type isn't known.}
- assign (FileCheck, readfile);
- reset (FileCheck);
- while (not eof(FileCheck)) and (not CROk) and (not LFOk) do
-
- begin {Look for CR or LF}
- read (FileCheck, ReadBuf);
- if ReadBuf = CR then {CR found?}
- begin
- CROk := True; {If yes, set the CR flag.}
- Read (FileCheck, ReadBuf); {and get next char}
- if ReadBuf = LF then {next one a LF?}
- LFOk := True; {Flag it as found.}
- if CROk and LFOk then {So is it CR/LF?}
- begin
- checktype := dos; {If yes, specify DOS, and exit.}
- close (FileCheck);
- exit;
- end;
- end;
- if ReadBuf = LF then {Found a LF?}
- begin
- checktype := unix; {If yes, assume unix.}
- close (FileCheck); {Close and exit.}
- exit;
- end;
- end;
- if checkvar = bad then {If there was a problem:}
- begin
- writeln ('Ambiguous file type. Can''t determine type.');
- close (FileCheck);
- halt(2);
- end;
- end;
-
- procedure dos2unix (infile, outfile : string);
-
- var
- intext, outtext : text;
- ReadBuf1, ReadBuf2 : char;
-
- begin
- writeln ('Converting DOS -> UNIX. . . .');
- assign (intext, infile);
- reset (intext);
- assign (outtext, outfile);
- rewrite (outtext);
- while not eof(intext) do
- begin
- read (intext, ReadBuf1); {Get character}
- if ReadBuf1 = CR then {If it's CR then. . . }
- begin
- read (intext, ReadBuf2); {. . . get next . . .}
- if ReadBuf2 = LF then {. . . and see if it's LF.}
- write (outtext, LF) {If yes, just put LF into new file.}
- else
- write (outtext, ReadBuf1, ReadBuf2); {Not CR/LF, dump to file.}
- end
- else
- write (outtext, ReadBuf1); {Dump the character to file.}
- end;
- close (intext);
- close (outtext);
- end;
-
- procedure unix2dos (infile, outfile : string);
-
- var
- intext, outtext : text;
- ReadBuf : char;
-
- begin
- writeln ('Converting UNIX -> DOS. . . .');
- assign (intext, infile);
- reset (intext);
- assign (outtext, outfile);
- rewrite (outtext);
- while not eof(intext) do
- begin
- read (intext, ReadBuf); {Get a character.}
- if ReadBuf = LF then {Is it LF?}
- write (outtext, CR+LF) {If yes, put a CR/LF in its place.}
- else
- write (outtext, ReadBuf); {Otherwise, replace the character.}
- end;
- close (intext);
- close (outtext);
- end;
-
- procedure getcommandline;
-
- {get commandline info. . . .}
-
- var
- pnum : byte; {paramater counter}
- pstr : string[2]; {string snippet}
- fname : string; {temporary string}
-
- begin
- if (paramcount < 1) or (paramcount > 3) then
- help (false); {too few, too many--show help}
- infile := ''; {Init names.}
- outfile := '';
- force := bad;
- for pnum := 1 to paramcount do {Do this in two passes.}
- begin {#1.) Flags}
- pstr := paramstr(pnum); {Get parameter.}
- pstr[2] := upcase(pstr[2]);
- if pstr[1] in ['-', '/'] then {Flag?}
- case pstr[2] of
- 'H', '?' : help (false); {Is help.}
- 'D' : force := dos; {Is force DOS.}
- 'U' : force := unix; {Is force UNIX.}
- else
- help (true); {Bad switch.}
- end;
- end;
- for pnum := 1 to paramcount do {#2.) Filenames}
- begin
- fname := paramstr(pnum); {Get parameter.}
- if not (fname[1] in ['-', '/']) then
- begin {If not flag then}
- if infile = '' then {Get infile}
- infile := fname
- else if (infile <> '') and (outfile = '') then
- outfile := fname {Get outfile}
- else
- help (false); {Oops, too many.}
- end;
- end;
- end;
-
- begin
- getcommandline; {Parse parameters}
- sysID := checktype (infile); {Check the input file type}
- if sysID = force then {If it's getting forced, then}
- begin {compare types and skip if same.}
- write ('Input file is already type ');
- case sysID of
- dos : write ('DOS');
- unix : write ('UNIX');
- end;
- writeln (', skipped.');
- halt(5);
- end;
- case sysID of
- dos : dos2unix (infile, outfile); {DOS -> UNIX}
- unix : unix2dos (infile, outfile); {UNIX -> DOS}
- bad : begin {Not likely to happen but. . . .}
- writeln ('Internal error! Check source code and recompile.');
- halt (6);
- end;
- end;
- end.